package map;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @classname:Strream
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/15 21:56
 * @version: 0.1
 * @since:1.8
 */
public class StreamDemo {
    public static void main(String[] args) {
//       distints();
//        filters();
//        flatMaps();
//        maps();
            demo5();

    }

    private static void demo5() {
        List<Integer> score = Arrays.asList(98, 100, 76);
        //求和求平均分最值   reduce  通过get获取
        Integer sum = score.parallelStream().reduce(Integer::sum).get();
        System.out.println(sum);
        //求最大值
        Integer max = score.parallelStream().reduce(Integer::max).get();
        System.out.println(max);

        //截断limit()
        List<Integer> collect = score.parallelStream().limit(2).collect(Collectors.toList());
        System.out.println(collect);


    }

    private static void maps() {
        List<User> list = new ArrayList<>(10);
        Collections.addAll(list,
                new User(1, "JIM", 21),
                new User(2, "lucy", 25),
                new User(3, "lik", 12),
                new User(4, "kim", 18)
        );
        //将集合内多有对象的年龄加1
        List<Integer> collect = list.parallelStream().map(User -> User.getAge() + 1).collect(Collectors.toList());
        System.out.println(collect);
        //
        List<String> strlist = Arrays.asList("s", "sdsS", "sadSAWW", "sdsSa");
        //进集合中的字母都转换成大写或者小写
        List<String> collect1 = strlist.parallelStream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(collect1);
        //将集合中每个字符后面加一个单词
        List<String> zhao = strlist.parallelStream().map(str -> str.concat("zhao")).collect(Collectors.toList());
        System.out.println(zhao);


    }

    private static void flatMaps() {
        //一对多
        List<Integer> list = Arrays.asList(1, 2, 3, 2, 1);
        List<Integer> list1 = Arrays.asList(1, 2, 1);
        List<Integer> list2 = Arrays.asList(1, 22, 3, 12, 10);
//将三个集合放到一起并且去重
        List<Integer> collect = Stream.of(list, list1, list2).flatMap(childList -> childList.stream()).distinct().collect(Collectors.toList());
        System.out.println(collect);
    }




    private static void filters() {
        List<User> list = new ArrayList<>(10);
        Collections.addAll(list,
                new User(1, "JIM", 21),
                new User(2, "lucy", 25),
                new User(3, "lik", 12),
                new User(4, "kim", 18)
        );
//查询年龄大于20并放到一个集合之中
        //方法一；

    /*    List<User> list1 = new ArrayList<>(10);
        list.forEach(user->{
            if(user.getAge()>20){
                list1.add(user);
            }
        });

        System.out.println(list1);*/

        //方式二用stream中的filter方法
        List<User> collect = list.parallelStream().filter(user -> user.getAge() > 20).collect(Collectors.toList());
        System.out.println(collect);
        //名字中包含l字符的
        List<User> l = list.parallelStream().filter(user -> user.getName().contains("l")).collect(Collectors.toList());
        System.out.println(l);

        //两个条件也可以写
//        List<User> collect = list.parallelStream().filter(user -> user.getAge() > 20||user.getId()>2).collect(Collectors.toList());


    }

    private static void distints() {
        //创建list集合
        List<Integer> list = Arrays.asList(1, 12, 2, 1, 2, 21, 34, 3, 23);

        //去重方法一：创建新的数组循环遍历通过是否包含来添加元素

      /*  List<Integer> list1  = new ArrayList<>();
        list.forEach(num->{
            if(!list1.contains(num)){
                list1.add(num
                );
            }
        });
        System.out.println(list1);
*/

        //去重方法二
        //转换成set集合去重
       /* LinkedHashSet<Integer> set = new LinkedHashSet<>(list);
        System.out.println(set);
*/


        //获取stream流进行去重--->手机去重之后的数据
        List<Integer> collect = list.parallelStream().distinct().collect(Collectors.toList());
        System.out.println(collect);

    }
}
